package com.example.schedule.quartz.base;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

import static org.quartz.DateBuilder.dateOf;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/2/8 18:09
 */
public class Test {
    public static void main(String[] args) throws SchedulerException {
        //创建一个scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("skey", "svalue");
        //创建一个job
        JobDetail job = JobBuilder.newJob(MyJob.class)
                .usingJobData("j1", "jv1")

                .withIdentity("myjob", "mygroup").build();
        job.getJobDataMap().put("j2", "jv2");

        JobDetail job1 = JobBuilder.newJob(MyJob.class).build();
        job1.getJobDataMap().put("jb1","我是job1的附加信息");
                //.usingJobData()

        //创建一个Trigger
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .usingJobData("t1", "tv1")
               // .withSchedule(DateBuilder.)
                .withSchedule(simpleSchedule().withIntervalInSeconds(3)

                        .withMisfireHandlingInstructionNextWithExistingCount()

                        .repeatForever()
                        .withMisfireHandlingInstructionFireNow()
                )
                .forJob(job1)

                .startAt(new Date())
                .endAt(dateOf(1,1,1,2, 3,2021))
                .withPriority(1)


                .build();
        trigger.getJobDataMap().put("t2", "tv2");


        //Trigger.MISFIRE_INSTRUCTION_SMART_POLICY

        //SchedulerListenerSupport
        //注册trigger并启动scheduler
        scheduler.scheduleJob(job1,trigger);
//        scheduler.getListenerManager().addJobListener();
//        scheduler.getListenerManager().addSchedulerListener();
        scheduler.start();
        //scheduler.shutdown();
    }
}





